package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.NumberFormat;
import java.text.ParseException;

/**
 * 金额转换工具
 */
public final class MoneyUtil {

    private MoneyUtil(){}

    public static String fenToYuan(String amount){
        NumberFormat format = NumberFormat.getInstance();
        try{
            Number number = format.parse(amount);
            double temp = number.doubleValue() / 100.0;
            format.setGroupingUsed(false);
            // 设置返回的小数部分所允许的最大位数
            format.setMaximumFractionDigits(2);
            amount = format.format(temp);

        } catch (ParseException e){
            e.printStackTrace();
        }

        return amount;
    }
    public static Long yuanToFen(String amount){
        NumberFormat format = NumberFormat.getInstance();
        try{
            Number number = format.parse(amount);
            double temp = number.doubleValue() * 100.0;
            format.setGroupingUsed(false);
            // 设置返回数的小数部分所允许的最大位数
            format.setMaximumFractionDigits(0);
            amount = format.format(temp);
        } catch (ParseException e){
            e.printStackTrace();
        }
        return Long.valueOf(amount);
    }

    public static Long jiaoToFen(String amount){
        NumberFormat format = NumberFormat.getInstance();
        try{
            Number number = format.parse(amount);
            double temp = number.doubleValue() * 10.0;
            format.setGroupingUsed(false);
            // 设置返回数的小数部分所允许的最大位数
            format.setMaximumFractionDigits(0);
            amount = format.format(temp);
        } catch (ParseException e){
            e.printStackTrace();
        }
        return Long.valueOf(amount);
    }

    public static void main(String[] args) {
        System.out.println(fenToYuan("10"));
        JSONObject requestBody = new JSONObject();
        requestBody.put("start_time", 0);
        requestBody.put("end_time", 0);
        requestBody.put("page_size", 10);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add("success");
        requestBody.put("state", jsonArray.toJSONString());
        requestBody.put("shop_id_list", new JSONArray());
//        requestBody.put("sid", accountObj.getString("sid"));
        System.out.println(JSONObject.toJSONString(requestBody));
    }

}
